package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.user.PickMaterialClassEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class PickMaterialClassAdapter extends BaseRecycleAdapter<PickMaterialClassEntity> {

    private LinearLayout llProjectInfo;
    private LinearLayout llBuyCompany;
    private TextView tvBuyNum;
    private TextView mTvQuoteImmediately;
    private IOffer iOffer;

    public PickMaterialClassAdapter(Context context, List<PickMaterialClassEntity> datas) {
        super(context, R.layout.item_pick_material_class, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final PickMaterialClassEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        tvBuyNum = holder.getItem().findViewById(R.id.tv_buyNum);
        llProjectInfo = holder.getItem().findViewById(R.id.ll_projectInfo);
        llBuyCompany = holder.getItem().findViewById(R.id.ll_buyCompany);
        mTvQuoteImmediately = holder.getItem().findViewById(R.id.tv_quote_immediately);
        tvBuyNum.setText(entity.getNum() + entity.getUnit());

        llProjectInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editClickListener.onProjectInfoClick(entity, holder.getPosition());
            }
        });

        llBuyCompany.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editClickListener.onBuyCompanyClick(entity, holder.getPosition());
            }
        });
        if (entity.getProjectStatus() == 0) {
            if ("1".equals(entity.getOfferButtonStr())) {
                mTvQuoteImmediately.setText("再次报价");
            }
            mTvQuoteImmediately.setOnClickListener(view -> {
                iOffer.checkLogin(entity, layoutId);
            });
        } else {
            mTvQuoteImmediately.setText("项目结束");
            mTvQuoteImmediately.setClickable(false);
            mTvQuoteImmediately.setBackground(context.getResources().getDrawable(R.drawable.square_gray_bg));
            mTvQuoteImmediately.setTextColor(context.getResources().getColor(R.color.colorAccent));
        }
    }

    public interface IOffer {
        void checkLogin(PickMaterialClassEntity entity, int id);
    }

    public void IOffer(IOffer iOffer) {
        this.iOffer = iOffer;
    }

    private PickMaterialClassAdapter.onEditClickListener editClickListener;

    public void setonEditClick(PickMaterialClassAdapter.onEditClickListener editClickListener) {
        this.editClickListener = editClickListener;
    }

    public interface onEditClickListener {
        void onProjectInfoClick(PickMaterialClassEntity entity, int position);

        void onBuyCompanyClick(PickMaterialClassEntity entity, int position);
    }
}
